package de.distmlp.preprocessing.parser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import de.distmlp.preprocessing.data.MLDataEntry;
import de.distmlp.preprocessing.nlp.dictionary.Dictionary;

public class XingParser implements IParser {

	protected final Dictionary dictionary;
	protected final int nbOutputUnits;
	protected final int nbInputUnits;

	public XingParser(final Dictionary dictionary, final int nbInputUnits, final int nbOutputUnits) {
		this.dictionary = dictionary;
		this.nbInputUnits = nbInputUnits;
		this.nbOutputUnits = nbOutputUnits;
	}

	/**
	 * Parses a line containing several lemmas and creates a trainings entry for
	 * each lemma.
	 * 
	 * Input: java,eclipse,c++ Output: 0:1|1:1,2:1;1:1|0:1,2:1;2:1|0:1,1:1
	 */
	@Override
	public List<MLDataEntry> parse(final String input) {
		final List<MLDataEntry> trainingsSet = new ArrayList<MLDataEntry>();

		final Map<String, Integer> dicMap = this.dictionary.getDictionary();
		final String[] lineSplit = input.split(",");
		int count = 0;
		// Dictionary does not contain all lemmas. We need to check
		// if lemma exists in dictionary,
		if (lineSplit.length > 1) {
			for (int x = 0; x < lineSplit.length; x++) {
				final MLDataEntry trainingsData = new MLDataEntry(this.nbInputUnits, this.nbOutputUnits);

				if (dicMap.containsKey(lineSplit[x])) {
					trainingsData.addInput(dicMap.get(lineSplit[x]), 1);
				} else {
					continue;
				}
				for (int y = 0; y < lineSplit.length; y++) {
					if (x == y) {
						continue;
					}
					if (dicMap.containsKey(lineSplit[y])) {
						trainingsData.addOutput(dicMap.get(lineSplit[y]), 1);
						count++;
					}
				}
				if (count > 1) {
					trainingsSet.add(trainingsData);
				}
				count = 0;
			}
		}
		return trainingsSet;
	}
}
